package algotrader;

import java.util.HashMap;
import java.util.List;

import algotrader.book.Book;
import algotrader.book.BookLine;
import algotrader.book.BookManager;
import algotrader.book.BookRequester;

import quickfix.Application;
import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.SessionID;
import quickfix.UnsupportedMessageType;

public class FakeFixApplication implements IFixApplication, Application,BookRequester {
	private BookManager bookManager;
	private OperadorPregao operadorPregao;
	private HashMap<String,Order> orders = new HashMap<String, Order>();
	
	@Override
	public void receiveMarketData(Book book) {
		bookManager.receiveBook(book);
	}

	@Override
	public void requestBook(String string, BookManager bookManager) {
		this.bookManager = bookManager;
	}

	@Override
	public void receiveUpdateMarketData(BookLine bookLine) {
		bookManager.receiveBookLine(bookLine);
	}

	@Override
	public void receiveUpdateMarketData(List<BookLine> bls) {
		for(BookLine bl :bls)
			bookManager.receiveBookLine(bl);
	}

	@Override
	public void removeBookLine(BookLine bookLine) {
		bookManager.removeBookLine(bookLine);
	}

	@Override
	public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound,
			IncorrectDataFormat, IncorrectTagValue, RejectLogon {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound,
			IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCreate(SessionID arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLogon(SessionID arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLogout(SessionID arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void toAdmin(Message arg0, SessionID arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void toApp(Message arg0, SessionID arg1) throws DoNotSend {
		// TODO Auto-generated method stub
		
	}

	public void setOperadorPregao(OperadorPregao op) {
		this.operadorPregao = op;
	}

	@Override
	public void sendOrder(Order o) {
		orders.put(o.getId(), o);
	}

	public void executeOrder(Order o) {
		o.execute(o.getQty());
		this.operadorPregao.updateOrder(o.getId(), o);
	}

	public Order getOrder(String id) {
		return orders.get(id);
	}

	public void partialExecuteOrder(Order o, int i) {
		o.execute(i);
		this.operadorPregao.updateOrder(o.getId(), o);
	}

	public void activateStopLimitOrder(Order o) {
		o.setType(Order.OrderType.LIMIT);
		this.operadorPregao.updateOrder(o.getId(), o);
	}

	public void cancelOrder(Order o) {
		
		o.cancel();
		this.operadorPregao.updateOrder(o.getId(), o);
	}

	public void registerOrder(Order o) {
		o.setEstado(Order.Estado.REGISTRADA);
		this.operadorPregao.updateOrder(o.getId(), o);
	}

	@Override
	public void solicitaCancelamento(Order o) {
		this.orders.remove(o.getId());
	}

	@Override
	public void solicitaAlteracao(Order alterada) {
		// TODO Auto-generated method stub
		
	}

}
